/**
 * 
 */
package com.yidin.lsc;

import java.util.HashMap;
import java.util.Map;

import com.yidin.entry.AccountDetailEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 修改用户个人的资料 UserUpdateProfessionalInfo
 * 
 * @author cola
 * 
 */
public class EditMyData extends BaseActivity implements OnClickListener {
	private ViewGroup ViewContainer;
	protected LayoutInflater inflater;
	private View group;
	private Button rightBtn;
	private Button leftBtn;
	private TextView title;
	private EditText inputBox;

	private TextView IsMarry;
	private TextView Salary;
	private TextView Height;
	private TextView Homepage;

	private TextView Profession;
	private TextView School;
	private TextView Hobby;
	private TextView PalFor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editmydatacontainer);
		init();
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		inputBox = new EditText(this);
		inputBox.setTextSize(15f);
		// inputBox.setText("正在加载，请稍候...");
		inputBox.setTextColor(Color.rgb(100, 100, 100));
		RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,//
				150);
		inputBox.setLayoutParams(mParams);
		inputBox.setBackgroundResource(R.drawable.shape_bg);
		inputBox.setGravity(Gravity.CENTER);

		this.title = (TextView) findViewById(R.id.title);
		this.title.setText("修改资料");

		this.leftBtn = (Button) findViewById(R.id.leftBtn);
		this.leftBtn.setBackgroundDrawable(null);
		this.leftBtn.setText("取消");
		this.leftBtn.setTextColor(Color.WHITE);
		this.leftBtn.setOnClickListener(cancel);

		this.rightBtn = (Button) findViewById(R.id.rightBtn);
		this.rightBtn.setOnClickListener(OK);
		this.rightBtn.setBackgroundDrawable(null);
		this.rightBtn.setText("保存");
		this.rightBtn.setTextColor(Color.WHITE);

		// this.group = new HashMap<Button, View>();
		inflater = getLayoutInflater();
		ViewContainer = (ViewGroup) findViewById(R.id.ViewContainer);
		group = inflater.inflate(R.layout.editmydatacontent, null);
		IsMarry = (TextView) group.findViewById(R.id.IsMarry);
		IsMarry.setOnClickListener(this);
		Salary = (TextView) group.findViewById(R.id.Salary);
		Salary.setOnClickListener(this);
		Height = (TextView) group.findViewById(R.id.Height);
		Height.setOnClickListener(this);
		PalFor = (TextView) group.findViewById(R.id.PalFor);
		PalFor.setOnClickListener(this);

		Profession = (TextView) group.findViewById(R.id.Profession);
		Profession.setOnClickListener(this);
		School = (TextView) group.findViewById(R.id.School);
		School.setOnClickListener(this);
		Hobby = (TextView) group.findViewById(R.id.Hobby);
		Hobby.setOnClickListener(this);
		Homepage = (TextView) group.findViewById(R.id.Homepage);
		Homepage.setOnClickListener(this);
		ViewContainer.addView(group);
		ViewContainer.addView(inputBox);
		inputBox.setVisibility(View.GONE);

		Map obj = new HashMap<String, String>();
		obj.put("AccountFrom", "hlf");
		obj.put("AccountTo", "hlf");
		showProgress(this);//
		Task task = new Task(Task.TASK_ACCOUNT_DETAIL, obj);// TASK_ACCOUNT_DETAIL
		MainService.AddNewTask(task);// TASK_NEARBY_SHOPDETAIL
	}

	private OnClickListener cancel = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (group.getVisibility() == View.VISIBLE)
				finish();
			else {
				String temp = onTv.getText().toString();
				if (temp == null || temp.length() == 0 || temp.length() > 40) {
					Toast.makeText(EditMyData.this, "请检查输入的内容长度",
							Toast.LENGTH_LONG).show();
					return;
				}
				leftBtn.setText("取消");
				rightBtn.setText("保存");

				inputBox.setVisibility(View.GONE);
				group.setVisibility(View.VISIBLE);
			}
		}

	};

	private OnClickListener OK = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (group.getVisibility() == View.VISIBLE) {
				showProgress(EditMyData.this);//
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", "hlf");
				obj.put("Height", Height.getText().toString());
				obj.put("Hobby", Hobby.getText().toString());
				obj.put("Homepage", Homepage.getText().toString());
				obj.put("IsMarry", IsMarry.getText().toString());
				obj.put("PalFor", PalFor.getText().toString());
				obj.put("Profession", Profession.getText().toString());
				obj.put("Salary", Salary.getText().toString());
				obj.put("School", School.getText().toString());

				Task task = new Task(Task.TASK_USER_EDITDATA, obj);// TASK_ACCOUNT_DETAIL
				MainService.AddNewTask(task);// TASK_NEARBY_SHOPDETAIL
			} else {
				onTv.setText(inputBox.getText().toString());
				inputBox.setText("");
				leftBtn.setText("取消");
				rightBtn.setText("保存");

				inputBox.setVisibility(View.GONE);
				group.setVisibility(View.VISIBLE);
			}
			// TODO Auto-generated method stub

		}

	};

	private void updateData(AccountDetailEntry ad) {
		this.Height.setText(ad.Height);
		this.Hobby.setText(ad.Hobby);
		this.Homepage.setText(ad.Homepage);
		this.IsMarry.setText(ad.IsMarry);
		this.PalFor.setText(ad.PalFor);
		this.Profession.setText(ad.Profession);
		this.Salary.setText(ad.Salary);
		this.School.setText(ad.School);
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		this.dismissProgress();
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_ACCOUNT_DETAIL:
			AccountDetailEntry ad = (AccountDetailEntry) param[1];
			if (ad == null)
				return;
			if (!ad.e.equals("0000")) {
				Toast.makeText(EditMyData.this, ad.e, Toast.LENGTH_LONG).show();
				return;
			}
			updateData(ad);
			break;
		case Task.TASK_USER_EDITDATA:
			AccountDetailEntry ad2 = (AccountDetailEntry) param[1];
			if (ad2 == null)
				return;
			if (!ad2.e.equals("0000"))
				Toast.makeText(EditMyData.this, ad2.e, Toast.LENGTH_LONG)
						.show();
			else
				Toast.makeText(EditMyData.this, "更新成功", Toast.LENGTH_LONG)
						.show();
			break;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private TextView onTv;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		leftBtn.setText("返回");
		rightBtn.setText("确定");
		this.group.setVisibility(View.GONE);
		this.inputBox.setVisibility(View.VISIBLE);
		if (IsMarry == v) {
			this.onTv = (TextView) v;
		} else if (this.Salary == v) {
			this.onTv = (TextView) v;
		} else if (Height == v) {
			this.onTv = (TextView) v;
		} else if (this.PalFor == v) {
			this.onTv = (TextView) v;
		} else if (Profession == v) {
			this.onTv = (TextView) v;
		} else if (this.School == v) {
			this.onTv = (TextView) v;
		} else if (Hobby == v) {
			this.onTv = (TextView) v;
		} else if (this.Homepage == v) {
			this.onTv = (TextView) v;
		}
	}

}
